package com.course.domain.service.impl;

import com.course.domain.dao.CourseInfoDao;
import com.course.domain.dao.CoursePlanDao;
import com.course.domain.enums.TaskType;
import com.course.domain.model.CourseInfo;
import com.course.domain.model.CoursePlan;
import com.course.domain.service.CourseService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * Created by Justin on 2017/5/12.
 */
@Service
public class CourseServiceImpl implements CourseService {

    private final CourseInfoDao infoDao;
    private final CoursePlanDao planDao;

    @Autowired
    public CourseServiceImpl(CourseInfoDao infoDao,
                             CoursePlanDao planDao) {
        this.infoDao = infoDao;
        this.planDao = planDao;
    }

    @Override
    public CourseInfo createInfo(CourseInfo info) {
        return infoDao.save(info);
    }

    @Override
    public CourseInfo updateInfo(CourseInfo info) {
        return infoDao.save(info);
    }

    @Override
    public CourseInfo deleteInfo(CourseInfo info) {
        info.setStatus(0);
        return infoDao.save(info);
    }

    @Override
    public CoursePlan createPlan(CoursePlan plan) {
        return planDao.save(plan);
    }

    @Override
    public CoursePlan updatePlan(CoursePlan plan) {
        return planDao.save(plan);
    }

    @Override
    public CourseInfo loadInfo(int courseId) {
        return infoDao.findOne(courseId);
    }

    @Override
    public List<CourseInfo> listInfo() {
        return infoDao.findAllByStatus(1);
    }

    @Override
    public List<CourseInfo> findInfo(List<Integer> courseIds) {
        return infoDao.findAll(courseIds);
    }

    @Override
    public List<CoursePlan> findPlan(int courseId, int weedIdx) {
        return planDao.findByCourseIdAndWeekIdx(courseId, weedIdx);
    }

    @Override
    public List<CoursePlan> findPlanByType(int courseId, TaskType type) {
        return planDao.findByCourseIdAndTaskType(courseId, type);
    }

    @Override
    public CoursePlan loadPlanByCourseAndType(int courseId, int taskId, TaskType type) {
        return planDao.findByCourseIdAndTaskIdAndTaskType(courseId, taskId, type);
    }

    @Override
    public List<CoursePlan> findLearn(int courseId, int weekIdx, int weekDay) {
        return planDao.findByCourseIdAndWeekIdxAndWeekDay(courseId, weekIdx, weekDay);
    }
}
